﻿module TextSplitter

open System.Text.RegularExpressions
open ComputedStyle
open StyledElements
open Lines

let collapse (textNode:IStyledText) txt = 
    match textNode.computedStyle().textSpaceCollapse with
    | Collapse ->
        Regex.Replace(txt, @"\s+", " ")
    | PreserveBreaks ->
        let txt = Regex.Replace(txt, @"[ \t]+", " ")
        txt
    | Preserve ->
        txt

let split (textNode:IStyledText) txt =
    match textNode.computedStyle().textWrap with 
    | TextWrapNormal ->
        Regex.Split(txt, @"(?<=\s)")
    | TextWrapNone ->
        Regex.Split(txt, @"(?<=\n)")

let splitTextIntoWordUnits (textNode:IStyledText) =
    let parts = split textNode <| collapse textNode (textNode.nodeValue)
    [for str in parts -> WordUnit(str, textNode)]

